//
//  RankCell.m
//  WanCaiC8
//
//  Created by QiuXun on 2019/1/14.
//  Copyright © 2019 tangang. All rights reserved.
//

#import "RankCell.h"

@interface RankCell ()

@property(nonatomic,strong)UIImageView *icon;
@property(nonatomic,strong)QXLable     *titleLabel;
@property(nonatomic,strong)QXLable     *describeLabel;
@property(nonatomic,strong)QXLable     *timeLabel;

@property(nonatomic,strong)UIView      *line1;
@property(nonatomic,strong)UIView      *line2;




@end
@implementation RankCell{
    dispatch_source_t _timer;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
    
    UIView *bgView =[[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(8, 5, 5, 8 ));
    }];
    
    bgView.layer.cornerRadius = 5;
    bgView.layer.shadowColor = [UIColor blackColor].CGColor;
    bgView.layer.shadowOffset = CGSizeMake(0,0);
    bgView.layer.shadowOpacity = 0.7;
    bgView.layer.shadowRadius = 1;

    
    
    self.icon=[[UIImageView alloc]init];
    [bgView addSubview:self.icon];
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bgView);
        make.left.equalTo(bgView).offset(8);
        make.size.mas_equalTo(CGSizeMake(real_size(30), real_size(30)));
    }];
    
    self.titleLabel=[[QXLable alloc] init];
    self.titleLabel.textAlignment=NSTextAlignmentLeft;
    [bgView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.icon).offset(-5);
        make.left.equalTo(self.icon.mas_right).offset(8);
        make.right.equalTo(self.contentView);
        make.height.mas_equalTo(15);
        
    }];
    
    self.describeLabel=[[QXLable alloc] init];
    self.describeLabel.textAlignment=NSTextAlignmentLeft;
    self.describeLabel.font = [UIFont systemFontOfSize:10];
    self.describeLabel.textColor = DEFAULTTEXTGRAYCOLOR;
    [bgView addSubview:self.describeLabel];
    [self.describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(5);
        make.left.right.equalTo(self.titleLabel);
        make.height.mas_equalTo(10);
    }];
    
    
    self.timeLabel=[[QXLable alloc] init];
    self.timeLabel.textAlignment=NSTextAlignmentLeft;
    self.timeLabel.font = [UIFont systemFontOfSize:10];
    self.timeLabel.textColor = NAVCOLOR;
    [bgView addSubview:self.timeLabel];
    self.timeLabel.hidden = YES;
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.describeLabel.mas_bottom).offset(5);
        make.left.right.equalTo(self.titleLabel);
        make.height.mas_equalTo(15);
    }];
    
   
    
}

-(void)setModel:(RankListModel *)model{
    
    self.titleLabel.text=model.LTypeName;
    self.describeLabel.text=model.OpenTip;
    NSURL *url=[NSURL URLWithString:model.Logo];
    [self.icon sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"chat_icon"]];
    self.timeLabel.hidden = YES;
    

}

-(void)setHome_LotteryModel:(Home_LotteryModel *)home_LotteryModel{
    
    _home_LotteryModel = home_LotteryModel;
    self.titleLabel.text=home_LotteryModel.LTypeName;
    self.describeLabel.text=home_LotteryModel.OpenTip;
    NSURL *url=[NSURL URLWithString:home_LotteryModel.Logo];
    [self.icon sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"chat_icon"]];
    
    self.timeLabel.hidden = NO;
    self.timeLabel.text = home_LotteryModel.OpenTime;
    
    //启动定时器
    
    
    [self startTimer];
    
}

- (void)cancleTimer{
    if (_timer) {
        dispatch_source_cancel(_timer);
    }
}
- (void)startTimer{
    [self cancleTimer];
    //    if (self.selectLottery<0&&self.navArr.count>self.selectLottery) {
    //        return;
    //    }
    WS(weakSelf);
    __block int timeout = 0;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{

        
        
        
        if (![self.home_LotteryModel.OpenTime isEqualToString:@"正在开奖"]) {
            NSArray *timeArr=[self.home_LotteryModel.OpenTime componentsSeparatedByString:@":"];
            int hour=[timeArr[0] intValue];
            int minutes=[timeArr[1] intValue];
            int second=[timeArr[2] intValue];
            if (second>0) {
                second-=1;
                self.home_LotteryModel.OpenTime=[NSString stringWithFormat:@"%@:%@:%@",[TGTools getTime:hour],[TGTools getTime:minutes],[TGTools getTime:second]];
            }else if (minutes>0){
                minutes-=1;
                second=59;
                self.home_LotteryModel.OpenTime=[NSString stringWithFormat:@"%@:%@:%@",[TGTools getTime:hour],[TGTools getTime:minutes],[TGTools getTime:second]];
            }else if (hour>0){
                hour-=1;
                minutes=59;
                second=59;
                self.home_LotteryModel.OpenTime=[NSString stringWithFormat:@"%@:%@:%@",[TGTools getTime:hour],[TGTools getTime:minutes],[TGTools getTime:second]];
            }else{
                
                
                self.home_LotteryModel.OpenTime=@"正在开奖";
                
                if(timeout==20){
                    timeout=0;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        //发送通知让collection 刷新。重新赋值model
                        [self cancleTimer];
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"TGReloadData" object:nil];
                    });
                }else{
                    timeout++;
                }
                
            }
            
        }else{
            
            self.home_LotteryModel.OpenTime=@"正在开奖";
            
            if(timeout==20){
                timeout=0;
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [self cancleTimer];

                    //发送通知让collection 刷新。重新赋值model
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"TGReloadData" object:nil];
                });
                
            }else{
                timeout++;
            }
            
            
           
            
        }
        
        
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.timeLabel.text=[NSString stringWithFormat:@"%@",self.home_LotteryModel.OpenTime];
        });
        
    });
    dispatch_resume(_timer);
}



- (void)dealloc
{
    NSLog(@"------Rankcell销毁");
    if (_timer) {
        dispatch_source_cancel(_timer);
    }
}
@end
